<?php
namespace Modules\Agent\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Agent\Entities\Agent;

use Validator;
use ThrowException;
use DB;

class AgentController extends Controller
{
    public function statusTotal(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $options = $req->all();

        $total = [
            'level1' => Agent::getBuilder($options)->count(),
            'blocked' => Agent::getBuilder($options)->blocked()->count()
        ];

        return [
            'total' => $total
        ];
    }

    // 用户列表
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $options['with'] = ['inviter', 'agent_level'];
        // $options['with_count'] = ['invitees'];

        // if ($req->input('sort')) {
        //     if ($req->sort === 'invitee')
        //         $options['order_by'] = ['invitees_count' => 'desc'];
        // }
        $options['order_by'] = ['completed_money_brokerage' => 'desc'];

        $options['only'] = [
            'id',
            'uuid',
            'name',
            'real_name',
            'number',
            'card_number',
            'phone',
            'gender',
            'headimg',
            'email',
            'birthday',
            'city',
            'area',
            'score',
            'level_score',
            'agent_level_index',
            'redpack',
            'level_text',
            'client_type',
            'status',
            'invitees_count',
            'last_active_at',
            'created_at',
            'remark',
            'tags',
            'agent_level',
            'marked_at',
            'direct_invitees_count',
            'indirect_invitees_count',
            'pending_money_brokerage',
            'pending_score_brokerage',
            'completed_money_brokerage',
            'completed_score_brokerage',
            'agent_status',
            'inviter' => [
                'id',
                'uuid',
                'name',
                'headimg'
            ],
            'agent_level'
        ];

        $list = Agent::getList($options);

        $list->each(function ($item) {
            $item->score = $item->asset('score')->balance;
            $item->level_score = $item->asset('level_score')->balance;
            $item->redpack = $item->asset('redpack')->balance;
        });

        return $list->generateListResponse();
    }

    // 单个用户详情
    public function show(Request $req, $uuid)
    {
        $item = Agent::getEntity([
            'uuid' => $uuid,
            'with' => [
                'ext',
                'skus'
            ]
        ]);

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    }

    // 更新用户信息
    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Agent::getEntity([
            'uuid' => $uuid
        ]);

        DB::beginTransaction();

        if ($req->type === 'update_agent_level') {
            $item->update([
                'agent_level_index'  =>  $req->agent_level
            ]);

            $item->checkAgentLevelUpdate();
        }
        elseif ($req->type === 'update_agent_status') {
            $item->update([
                'agent_status' => $req->agent_status
            ]);
        }
        elseif ($req->type === 'block') {
            $item->update([
                'status' => 2,
                'blocked_at' => now()
            ]);
        }
        elseif ($req->type === 'unblock') {
            $item->update([
                'status' => 1,
                'blocked_at' => null
            ]);
        }

        DB::commit();

        return [];
    }
}
